import React from 'react'
import axios from 'axios'
import CSSModules from 'react-css-modules'
import styles from './css/dashboard.scss'
import Nav from './components/Nav'
import RouterList from './router'
import BottomBar from './components/BottomBar'
import { setToken } from './util/util'
const shouldShowNav = ['/', '/account', '/mywk']

class Dashboard extends React.Component{
    componentDidMount(){
        if(this.props.history.location.pathname.indexOf('/account/auth/') === 0){
            return
        }

        axios.get('/api/account/self').then((response) => {
            if(response.data.code === 0) {
                localStorage.setItem('lzwk_id', Number(response.data.data.id));

                if (response.data.data.token) {
                    setToken(response.data.data.token)
                }
            }
        }).catch((error) => {
            console.log('failed to load self, error=', error);
        });
    }

    render(){
        const {history} = this.props
        const urlNow = this.props.history.location.pathname
        return (
            <div styleName="main-page">
                <RouterList history={history}/>
                {shouldShowNav.indexOf(urlNow) >-1 ? <BottomBar/> : null}
                {shouldShowNav.indexOf(urlNow) >-1 ? <Nav history={history}/> : null} 
            </div>
        )
    }
}

export default CSSModules(Dashboard, styles);